package com.guangwei.phoneforelder

import android.app.Activity
import java.util.ArrayList

class ActivityStack {
    private val activities = ArrayList<Activity>()

    fun getInstance(): ActivityStack {
        return this
    }

    val topActivity:Activity?
        get() = if (activities.isEmpty()) {
        null
    } else activities.get(activities.size - 1)

    fun addActivity(activity:Activity) {
        activities.add(activity)
    }

    fun finishTopActivity() {
        if (!activities.isEmpty()) {
            activities.removeAt(activities.size - 1).finish()
        }
    }

     fun finishActivity(activity:Activity?) {
         if (activity != null) {
             activities.remove(activity)
             activity.finish()
         }
     }

     fun finishActivity(activityClass:Class<*>) {
         for (activity in activities) {
             if (activity.javaClass == activityClass) {
                 finishActivity(activity)
             }
         }
     }

    fun finishAllActivity() {
        if (!activities.isEmpty()) {
            for (activity in activities) {
                activity.finish()
                activities.remove(activity)
            }
        }
    }

    companion object {
        val instance = ActivityStack()
    }
}
